<?php
/**
 * Created by xiao.M.
 * User: xiao.M.
 * Date: 2018/2/2
 * Time: 10:42
 */

class MyUtil extends CommonAction
{
    /**
     * 参数设置 叁数的拼接
     * @param $strA
     * @return string
     */
    public function parameterSql($strA) {
        if(!is_array($strA)) $strA=array($strA);
        $sql = '';
        foreach ($strA as $k) {
            if(!isset($_POST[$k])) continue;
            $sql .= ", " . $k . "='" . trim($_POST[$k]) . "'  ";
        }

        return $sql . ' ';
    }

    /**
     * 会员确认充值
     * @return [type] 异步请求
     */
    public function currencyPicUpload($data){
        $user_id = trim($this->_post('tousername'));
        $ePoints = trim($this->_post('price'));

        $zhifubao = trim($this->_post('zhifubao'));

        $type = trim($this->_post('type'));
        $chongzhi = M('chongzhi');

        if (empty($zhifubao)){
            $this->error ('请上传打款凭证');
            exit;
        }

        $id =  $_SESSION[C('USER_AUTH_KEY')];

        $data = array();
        $data['uid']     = $id;
        $data['user_id'] = $inUserID;
        $data['huikuan'] = $ePoints;
        $data['zhuanghao'] = $zhifubao;
        $data['rdt']     = time();
        $data['epoint']  = $ePoints;
        $data['is_pay']  = 0;
        $data['stype']  = $type;

        $rs2 = $chongzhi->add($data);
        unset($data,$id);
    }

    public function getUserLevelName($id) {
        $fck = M('fck')->where(['id' => $id])->find();

        $n_level = 0;
        if(3 == $fck['is_agent']) {
            $n_level = 1;
        } elseif (2 == $fck['is_agent'] && 1 == $fck['is_pay']) {
            $n_level = 2;
        } elseif (0 == $fck['is_agent'] && 1 == $fck['is_pay'] && $fck['cpzj'] > 0) {
            $n_level = 3;
        } else {
            $n_level = 4;
        }
        $name_a = C('USER_LEVEL_NAME');
        $name = explode('|', $name_a);

        return $name[$n_level - 1];
    }

    public function autoUpToAgent($id) {
        $fee = $this->getFeeRule();
        $money_agent = $fee['auto_to_agent'];
        $fck_s = M('fck')->where(['id' => $id, 'xf_money' => ['egt', $money_agent], 'is_pay' => 1, 'is_agent' => ['lt', 2]]);
        $fck = $fck_s->find();
        if(!$fck || empty($fck)) return false;

        return M('fck')->where(['id' => $id, 'xf_money' => ['egt', $money_agent], 'is_pay' => 1, 'is_agent' => ['lt', 2]])
                ->setField(['is_agent' => 2, 'cpzj' => $fee['agent_cpzj'], 'shoplx' => 1]);
    }
}